package cn.xabad.server.thea.test

import cn.xabad.server.thea.controller.IndexController
import cn.xabad.server.thea.http.AppLogService
import feign.Feign
import feign.gson.GsonDecoder
import feign.gson.GsonEncoder
import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.FileInputStream
import java.io.FileOutputStream
import java.text.DecimalFormat

val logger: Logger = LoggerFactory.getLogger(IndexController::class.java)
fun main1(args: Array<String>) {
    val appLogService = Feign.builder().encoder(GsonEncoder()).decoder(GsonDecoder())
            .requestInterceptor {
                println("http://120.27.244.103:9302" + it.url() + it.queryLine())
            }
            .target(AppLogService::class.java, "http://120.27.244.103:9302")
    val df = DecimalFormat("0")
    val file = "/Users/xueyong/Downloads/副本6.4练习未排队.xlsx"
    val fileInputStream = FileInputStream(file)
    val workbook = XSSFWorkbook(fileInputStream)
    val sheet = workbook.getSheetAt(1)
    println(sheet.sheetName)
    IntRange(1, sheet.physicalNumberOfRows - 1).forEach {
        val row = sheet.getRow(it)
        val idCell = row.getCell(0)
        val timeCell = row.getCell(2)
        idCell.setCellType(CellType.STRING)
        var id = idCell.stringCellValue
        var startTime = 0L
        var endTime = 0L
        var day = ""
//        id = df.format(idCell.stringCellValue)
        println(id)
        val format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
        val time = DateTime.parse(timeCell.stringCellValue,format)

        day = time.toString("yyyyMMdd")
        startTime = time.millis
        endTime = time.millis + (1000 * 60 * 5)
        if (time.hourOfDay < 18 ||
                time.hourOfDay > 22) {
            var cell: XSSFCell? = row.getCell(3)
            if (cell == null) {
                cell = row.createCell(3)
            }
            cell?.setCellValue("不在练习开启时间内")
            return@forEach
        }
        val log = appLogService.queryLog(
                id, day, startTime = startTime, endTime = endTime,
                page = 0, size = 100,
                dataAccessToken = "bf4472bf9bb2e63e15303730248c43d34d3652e42e58071bcbaf1bbfc6e362db")
        var starting = false
        if (log.data.content.isEmpty()) {
            var cell: XSSFCell? = row.getCell(3)
            if (cell == null) {
                cell = row.createCell(3)
            }
            cell?.setCellValue("没有埋点")
        } else {
            loop@ for (item in log.data.content) {
                when (item.type) {
                    "profile_set" -> {
                    }
                    "track" -> {
                        when (item.key) {
                            "boxfish_debug" -> {
                                if (starting) {
                                    val content = item.properties.debug_message ?: ""
                                    logger.debug(content)
                                    if (content.contains("弹出了视频介绍") ||
                                            content.contains("弹出了相机权限弹框") ||
                                            content.contains("显示微课视频介绍页")
                                    ) {
                                        var cell: XSSFCell? = row.getCell(3)
                                        if (cell == null) {
                                            cell = row.createCell(3)
                                        }
                                        cell?.setCellValue("弹出权限弹框(介绍视频)之后退出")
                                        break@loop
                                    } else if (content.contains("弹出了不在时间范围弹框")) {
                                        var cell: XSSFCell? = row.getCell(3)
                                        if (cell == null) {
                                            cell = row.createCell(3)
                                        }
                                        cell?.setCellValue("不在练习开启时间内")
                                        break@loop
                                    }
                                }

                            }
                            "finish_module_achieve" -> {
                            }

                            "student_button_click" -> {
                                if (!starting) {
                                    if ("talk_learn_5mcourse" == item.properties.button) {
                                        starting = true
                                    }
                                }
                            }
                            "finish_module" -> {
                            }
                            "online_course_heartbeat" -> {

                            }
                            "student_template_learning_video_open_question" -> {
                            }
                            else -> {
                            }
                        }
                    }
                    else -> {

                    }
                }
            }
        }

        fileInputStream.close()
        val fileOut = FileOutputStream(file)
        workbook.write(fileOut)
        fileOut.flush()
        fileOut.close()
    }
}